#include <iostream>
#include <memory>
using namespace std;

class demo_class
{
public:
    demo_class(int x) : value(x) {}
    void print() const { std::cout << "Value: " << value << std::endl; }

private:
    int value;
};

shared_ptr<demo_class> test1()
{
    shared_ptr<demo_class> p1 = make_shared<demo_class>(5);
    cout << "p1 use_count: " << p1.use_count() << " address: " << p1.get() << endl;

    return p1;
}

shared_ptr<demo_class> test2()
{
    auto p2 = test1();
    cout << "p2 use_count: " << p2.use_count() << " address: " << p2.get() << endl;

    return p2;
}

int main()
{
    auto p3 = test2();
    cout << "p3 use_count: " << p3.use_count() << " address: " << p3.get() << endl;

    auto p4 = test2();
    cout << "p4 use_count: " << p4.use_count() << " address: " << p4.get() << endl;

    p3.reset();
    cout << "p3 use_count: " << p3.use_count() << " address: " << p3.get() << endl;
    cout << "p4 use_count: " << p4.use_count() << " address: " << p4.get() << endl;

    return 0;
}

/* 测试结果：
p1 use_count: 1 address: 0x4dedc90
p2 use_count: 1 address: 0x4dedc90
p3 use_count: 1 address: 0x4dedc90
p1 use_count: 1 address: 0x4dee130
p2 use_count: 1 address: 0x4dee130
p4 use_count: 1 address: 0x4dee130
p3 use_count: 0 address: 0
p4 use_count: 1 address: 0x4dee130

结论: p1和p2指向同一个对象，p3和p4指向同一个对象，p3和p4的生命周期是独立的
 */